#pragma once

#include "OpenGL.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include < stdlib.h >
#include < vcclr.h >

namespace webCube {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	using namespace OpenGLForm;

	/// <summary>
	/// Summary for Form1
	///
	/// WARNING: If you change the name of this class, you will need to change the
	///          'Resource File Name' property for the managed resource compiler tool
	///          associated with all .resx files this class depends on.  Otherwise,
	///          the designers will not be able to interact properly with localized
	///          resources associated with this form.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		
		OpenGLForm::COpenGL ^ OpenGL;
	
		
		Form1(void)
		{
			OpenGL = gcnew COpenGL(this, 600, 300);
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
			std::ofstream myfile;
			 myfile.open("done2.txt", std::ios::app);
			 myfile << "FORM1 CONSTRUCTOR\n";
		     myfile.close();
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}	
	private: System::Windows::Forms::TextBox^  address;
	private: System::Windows::Forms::Button^  Go;
	protected: 

		private: System::Windows::Forms::Timer^  timer1;

	protected: 

	private: System::Windows::Forms::WebBrowser^  browser;
	private: System::Windows::Forms::Button^  Back;
	private: System::Windows::Forms::Button^  Forward;
	private: System::Windows::Forms::Button^  Refresh;
	private: System::Windows::Forms::MenuStrip^  Menu;

	private: System::Windows::Forms::ToolStripMenuItem^  closeToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  closeToolStripMenuItem1;
	private: System::Windows::Forms::ToolStripMenuItem^  aboutToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  aboutToolStripMenuItem1;
	private: System::Windows::Forms::StatusStrip^  Status;
	private: System::Windows::Forms::ToolStripStatusLabel^  StatusText;
	private: System::Windows::Forms::ToolStripProgressBar^  toolStripProgressBar1;
	private: System::Windows::Forms::TextBox^  WBstatus;
	private: System::Windows::Forms::TextBox^  imageList;






	private: System::ComponentModel::IContainer^  components;


#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->components = (gcnew System::ComponentModel::Container());
			this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
			this->address = (gcnew System::Windows::Forms::TextBox());
			this->Go = (gcnew System::Windows::Forms::Button());
			this->browser = (gcnew System::Windows::Forms::WebBrowser());
			this->Back = (gcnew System::Windows::Forms::Button());
			this->Forward = (gcnew System::Windows::Forms::Button());
			this->Refresh = (gcnew System::Windows::Forms::Button());
			this->Menu = (gcnew System::Windows::Forms::MenuStrip());
			this->closeToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->closeToolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->aboutToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->aboutToolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripMenuItem());
			this->Status = (gcnew System::Windows::Forms::StatusStrip());
			this->StatusText = (gcnew System::Windows::Forms::ToolStripStatusLabel());
			this->toolStripProgressBar1 = (gcnew System::Windows::Forms::ToolStripProgressBar());
			this->WBstatus = (gcnew System::Windows::Forms::TextBox());
			this->imageList = (gcnew System::Windows::Forms::TextBox());
			this->Menu->SuspendLayout();
			this->Status->SuspendLayout();
			this->SuspendLayout();
			// 
			// timer1
			// 
			this->timer1->Enabled = true;
			this->timer1->Interval = 10;
			this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
			// 
			// address
			// 
			this->address->Location = System::Drawing::Point(29, 68);
			this->address->Name = L"address";
			this->address->Size = System::Drawing::Size(318, 20);
			this->address->TabIndex = 0;
			this->address->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::address_KeyDown);
			// 
			// Go
			// 
			this->Go->Location = System::Drawing::Point(355, 68);
			this->Go->Name = L"Go";
			this->Go->Size = System::Drawing::Size(53, 21);
			this->Go->TabIndex = 1;
			this->Go->Text = L"Go";
			this->Go->UseVisualStyleBackColor = true;
			this->Go->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// browser
			// 
			this->browser->Location = System::Drawing::Point(28, 108);
			this->browser->MaximumSize = System::Drawing::Size(1000, 500);
			this->browser->MinimumSize = System::Drawing::Size(100, 100);
			this->browser->Name = L"browser";
			this->browser->Size = System::Drawing::Size(426, 233);
			this->browser->TabIndex = 2;
			this->browser->Navigated += gcnew System::Windows::Forms::WebBrowserNavigatedEventHandler(this, &Form1::browser_Navigated);
			this->browser->DocumentCompleted += gcnew System::Windows::Forms::WebBrowserDocumentCompletedEventHandler(this, &Form1::completed);
			// 
			// Back
			// 
			this->Back->Location = System::Drawing::Point(29, 31);
			this->Back->Name = L"Back";
			this->Back->Size = System::Drawing::Size(90, 26);
			this->Back->TabIndex = 3;
			this->Back->Text = L"Back";
			this->Back->UseVisualStyleBackColor = true;
			this->Back->Click += gcnew System::EventHandler(this, &Form1::Back_Click);
			// 
			// Forward
			// 
			this->Forward->Location = System::Drawing::Point(137, 31);
			this->Forward->Name = L"Forward";
			this->Forward->Size = System::Drawing::Size(80, 26);
			this->Forward->TabIndex = 4;
			this->Forward->Text = L"Forward";
			this->Forward->UseVisualStyleBackColor = true;
			this->Forward->Click += gcnew System::EventHandler(this, &Form1::Forward_Click);
			// 
			// Refresh
			// 
			this->Refresh->Location = System::Drawing::Point(244, 34);
			this->Refresh->Name = L"Refresh";
			this->Refresh->Size = System::Drawing::Size(76, 22);
			this->Refresh->TabIndex = 5;
			this->Refresh->Text = L"Refresh";
			this->Refresh->UseVisualStyleBackColor = true;
			this->Refresh->Click += gcnew System::EventHandler(this, &Form1::Refresh_Click);
			// 
			// Menu
			// 
			this->Menu->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->closeToolStripMenuItem, 
				this->aboutToolStripMenuItem});
			this->Menu->Location = System::Drawing::Point(0, 0);
			this->Menu->Name = L"Menu";
			this->Menu->Size = System::Drawing::Size(935, 24);
			this->Menu->TabIndex = 6;
			this->Menu->Text = L"menuStrip1";
			// 
			// closeToolStripMenuItem
			// 
			this->closeToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->closeToolStripMenuItem1});
			this->closeToolStripMenuItem->Name = L"closeToolStripMenuItem";
			this->closeToolStripMenuItem->Size = System::Drawing::Size(35, 20);
			this->closeToolStripMenuItem->Text = L"File";
			// 
			// closeToolStripMenuItem1
			// 
			this->closeToolStripMenuItem1->Name = L"closeToolStripMenuItem1";
			this->closeToolStripMenuItem1->Size = System::Drawing::Size(111, 22);
			this->closeToolStripMenuItem1->Text = L"Close";
			this->closeToolStripMenuItem1->Click += gcnew System::EventHandler(this, &Form1::closeToolStripMenuItem1_Click);
			// 
			// aboutToolStripMenuItem
			// 
			this->aboutToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->aboutToolStripMenuItem1});
			this->aboutToolStripMenuItem->Name = L"aboutToolStripMenuItem";
			this->aboutToolStripMenuItem->Size = System::Drawing::Size(40, 20);
			this->aboutToolStripMenuItem->Text = L"Help";
			// 
			// aboutToolStripMenuItem1
			// 
			this->aboutToolStripMenuItem1->Name = L"aboutToolStripMenuItem1";
			this->aboutToolStripMenuItem1->Size = System::Drawing::Size(114, 22);
			this->aboutToolStripMenuItem1->Text = L"About";
			// 
			// Status
			// 
			this->Status->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->StatusText, this->toolStripProgressBar1});
			this->Status->Location = System::Drawing::Point(0, 756);
			this->Status->Name = L"Status";
			this->Status->Size = System::Drawing::Size(935, 22);
			this->Status->TabIndex = 7;
			this->Status->Text = L"Status";
			// 
			// StatusText
			// 
			this->StatusText->Name = L"StatusText";
			this->StatusText->Size = System::Drawing::Size(38, 17);
			this->StatusText->Text = L"Status";
			// 
			// toolStripProgressBar1
			// 
			this->toolStripProgressBar1->Name = L"toolStripProgressBar1";
			this->toolStripProgressBar1->Size = System::Drawing::Size(100, 16);
			// 
			// WBstatus
			// 
			this->WBstatus->Location = System::Drawing::Point(635, 69);
			this->WBstatus->Name = L"WBstatus";
			this->WBstatus->Size = System::Drawing::Size(100, 20);
			this->WBstatus->TabIndex = 8;
			// 
			// imageList
			// 
			this->imageList->Location = System::Drawing::Point(480, 108);
			this->imageList->Multiline = true;
			this->imageList->Name = L"imageList";
			this->imageList->Size = System::Drawing::Size(429, 150);
			this->imageList->TabIndex = 10;
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->BackColor = System::Drawing::SystemColors::Control;
			this->ClientSize = System::Drawing::Size(935, 778);
			this->Controls->Add(this->imageList);
			this->Controls->Add(this->WBstatus);
			this->Controls->Add(this->Status);
			this->Controls->Add(this->Refresh);
			this->Controls->Add(this->Forward);
			this->Controls->Add(this->Back);
			this->Controls->Add(this->browser);
			this->Controls->Add(this->Go);
			this->Controls->Add(this->address);
			this->Controls->Add(this->Menu);
			this->MainMenuStrip = this->Menu;
			this->Name = L"Form1";
			this->Text = L"webCube 0.1";
			this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
			this->Menu->ResumeLayout(false);
			this->Menu->PerformLayout();
			this->Status->ResumeLayout(false);
			this->Status->PerformLayout();
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

			private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
					 {
						browser->Navigate((String ^)"about:blank");
					 }
					 
					 
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
			 {
			     if (!address->Text->Equals(""))
				 {
					 OpenGL->pic->clearAll();
					 browser->Navigate(address->Text);
				 }
			 }
	private: System::Void address_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e)
			 {
			     	 if(e->KeyCode == System::Windows::Forms::Keys::Up)
				 {
					OpenGL->MoveForward(-0.1);
				 }
				 if(e->KeyCode == System::Windows::Forms::Keys::Down)
				 {
					OpenGL->MoveForward(0.1);
				 }
				 if(e->KeyCode == System::Windows::Forms::Keys::Right)
				 {
					OpenGL->MoveRight(0.1);
				 }
				 if(e->KeyCode == System::Windows::Forms::Keys::Left)
				 {
					OpenGL->MoveRight(-0.1);
				 }
			     	 if(e->KeyCode == System::Windows::Forms::Keys::Enter && !address->Text->Equals(""))
				 {
					 std::ofstream myfile;
					 myfile.open("done2.txt", std::ios::app);
					 myfile << "----------------------------------------------NAVIGATING\n";
					 WBstatus->Text = "Loading...";
					 OpenGL->pic->clearAll();
					 browser->Navigate(address->Text);
					 myfile.close();
					 
				 }
			 }


void printtofile(String^ str)
{
	std::ofstream myfile;
	myfile.open("source.txt");
	
   // Pin memory so GC can't move it while native function is called
   pin_ptr<const wchar_t> wch = PtrToStringChars(str);
   printf_s("%S\n", wch);

   // Conversion to char* :
   // Can just convert wchar_t* to char* using one of the 
   // conversion functions such as: 
   // WideCharToMultiByte()
   // wcstombs_s()
   // ... etc
   size_t convertedChars = 0;
   size_t  sizeInBytes = ((str->Length + 1) * 2);
   errno_t err = 0;
   char    *ch = (char *)malloc(sizeInBytes);

   err = wcstombs_s(&convertedChars, 
                    ch, sizeInBytes,
                    wch, sizeInBytes);
   if (err != 0)
      myfile << "wcstombs_s  failed!\n";

	myfile << "---------------------text:\n\n";
	myfile << ch;
	myfile << "---------------------\nend";
	myfile.close();
}



	private: System::Void Back_Click(System::Object^  sender, System::EventArgs^  e)
			 {
				 browser->GoBack();
			 }
private: System::Void browser_Navigated(System::Object^  sender, System::Windows::Forms::WebBrowserNavigatedEventArgs^  e)
		 {
		     address->Text = browser->Url->ToString();
		 }
private: System::Void Forward_Click(System::Object^  sender, System::EventArgs^  e)
		 {
			 browser->GoForward();
		 }
private: System::Void Refresh_Click(System::Object^  sender, System::EventArgs^  e)
		 {
			 browser->Refresh();
		 }
private: System::Void closeToolStripMenuItem1_Click(System::Object^  sender, System::EventArgs^  e)
		 {
			 Close();
		 }

		 // Updates StatusBar1 with the current browser status text.
private: System::Void Browser_StatusTextChanged( Object^ sender, System::EventArgs^ e )
		{
			StatusText->Text = browser->StatusText;
			Status->Text = browser->StatusText;
		}

		 	private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e)
			 {
			     
				 std::ofstream myfile;
				 myfile.open("done2.txt", std::ios::app);
				 myfile << "timer_tick\n";
			
				 UNREFERENCED_PARAMETER(sender);
				 UNREFERENCED_PARAMETER(e);
			     myfile << "about to render\n";
				 OpenGL->Render();
				 OpenGL->SwapOpenGLBuffers();
				 
				 myfile << "DONE timer_tick\n";
			     myfile.close();
			
			 }
private: System::Void completed(System::Object^  sender, System::Windows::Forms::WebBrowserDocumentCompletedEventArgs^  e)
		 {
			
			 WBstatus->Text = "Done it";
			 String^ source = browser->DocumentText;//->Body->Parent->OuterHtml;
			 
			HtmlDocument^ code = browser->Document;
			HtmlElementCollection^ list = code->Images;
			//->GetElementsByTagName("src");
			WBstatus->Text = "Adding images....";
			OpenGL->pic->addImages(list);
			
			WBstatus->Text = "added images:" + list->Count;
			
			for(int i = 0; i < list->Count; i++)
			{
				HtmlElement^ a = list[i];
				String^ s2 = a->GetAttribute("src");
				imageList->SelectionStart = imageList->Text->Length;
				String^ add = add->Concat(s2, "\n");
				imageList->SelectedText = add;
			}

			String^ path = "C:/source.txt";
			System::IO::File::WriteAllText(path, source);
		}
};
}

